<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button
          v-if="isAuth('op:init:store')"
          type="danger"
          @click="storeInitHandle()"
          :disabled="false"
        >店铺初始化</el-button>
        <el-button
          v-if="isAuth('op:updown:store')"
          type="danger"
          @click="storeUpDownHandle()"
          :disabled="false"
        >店铺上下架删除</el-button>
        <el-button
          v-if="isAuth('op:date:set')"
          type="danger"
          @click="storeDateHandle()"
          :disabled="false"
        >酒店日期过滤</el-button>
        <el-button
          v-if="isAuth('op:rates:send')"
          type="danger"
          @click="ratesSendHandle()"
          :disabled="false"
        >价格推送</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="dataList" border v-loading="dataListLoading" style="width: 100%;">
      <el-table-column prop="excuTime" header-align="center" align="center" label="开始执行时间"></el-table-column>
      <el-table-column prop="taskName" header-align="center" align="center" label="任务名称"></el-table-column>
      <el-table-column prop="status" header-align="center" align="center" label="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 0" size="small" type="info">执行中</el-tag>
          <el-tag v-else size="small" type="success">已完成</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="compAmount" header-align="center" align="center" label="完成数"></el-table-column>
      <el-table-column
        :formatter="tranCompPercent"
        prop="compPercent"
        header-align="center"
        align="center"
        label="完成百分比"
      ></el-table-column>
      <el-table-column prop="dtName" header-align="center" align="center" label="分销商"></el-table-column>
      <el-table-column prop="storName" header-align="center" align="center" label="店铺"></el-table-column>
      <el-table-column prop="compTime" header-align="center" align="center" label="完成时间"></el-table-column>
      <el-table-column prop="archive" header-align="center" align="center" label="归档标志">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.archive === 1" size="small" type="info">历史</el-tag>
          <el-tag v-else size="small" type="success">当前</el-tag>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    <date-view v-if="dateViewVisible" ref="dateView"></date-view>
    <rates-send-view v-if="ratesViewVisible" ref="ratesSendView"> </rates-send-view>
  </div>
</template>

<script>
import AddOrUpdate from './storeinit-add-or-update.vue'
import DateView from './storeinit-date.vue'
import RatesSendView from './storeinit-rates-send'
export default {
  data() {
    return {
      dataForm: {
        key: ''
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      addOrUpdateVisible: false,
      dateViewVisible: false,
      ratesViewVisible: false
    }
  },
  components: {
    AddOrUpdate,
    DateView,
    RatesSendView
  },
  activated() {
    this.getDataList()
  },
  methods: {
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/op/taskstatus/list'),
        method: 'get',
        params: this.$http.adornParams({
          'page': this.pageIndex,
          'limit': this.pageSize,
          'taskCode': 'STOREHOTEL'
        })
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.dataList = data.page.list
          this.totalPage = data.page.totalCount
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      })
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 店铺初始化
    storeInitHandle() {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(0)
      })
    },
    // 店铺酒店上下架
    storeUpDownHandle() {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(1)
      })
    },
    // 日期过滤
    storeDateHandle() {
      this.dateViewVisible = true
      this.$nextTick(() => {
        this.$refs.dateView.init(1)
      })
    },
    // 价格推送
    ratesSendHandle() {
      this.ratesViewVisible = true
      this.$nextTick(() => {
        this.$refs.ratesSendView.init(1)
      })
    },
    tranCompPercent(row, com) {
     let percent = ""
      if (row.compAmount === null || row.compAmount === undefined) {
        percent = 0 + "%"
      } else if (row.compAmount != null && row.compAmount != undefined && row.compAmount != 0) {
        let tem = (row.compPercent / row.compAmount) * 100 + ""
        let re = /([0-9]+\.[0-9]{2})[0-9]*/
        percent = tem.replace(re, "$1") + "%"
      } else if (row.status === 1 || row.compAmount === 0) {
        percent = "100%"
      } else {
        let tem = (row.compAmount / 380000) * 100
        let re = /([0-9]+\.[0-9]{2})[0-9]*/ + ""
        percent = tem.replace(re, "$1") + "%"
      }
      return percent
    }
  }
}
</script>
